<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>home</title>
    </head>
    <body>
        <p>所有的租赁</p>
        <form method="get" action="home.php">
            <input placeholder="请输入姓名" name="p"><button class="button_b">搜索</button>
        </form>
    </body>
    <style>
        .button_b{
            color: white;
            background-color: blue;
            border: none;
            border-radius: 5px;
        }
        .button_s{
            color: white;
            background-color: red;
            border: none;
            border-radius: 5px;
        }
    </style>
</html>
<?php

if(!isset($_REQUEST['p']) or empty($_REQUEST['p'])){
    include 'sql_config.php';
    $sql = 'SELECT * FROM rent';
    $res = $conn->query($sql);
    if($res->num_rows > 0){
        while($row = $res->fetch_assoc()){
            $start_time = new DateTime($row['start_time']);
            $deathline = new DateTime($row['deathline']);
            $timec = date_diff($start_time,$deathline);
            $time = $timec -> format('%a');
            echo("<p>租赁人:".$row['name']."<br />租赁物品:".$row['w']."<br />物品价格:".$row['jiage']."￥/日<br />"."租赁时长:$time 天<br />应付费:".$time*$row['jiage']."￥"."<br />租赁时间:".$row['start_time']."到".$row['deathline']."<br /><button class='button_b'"." onclick='location.href=(\"update.php?id=".$row['id']."\");'".">编辑</button><button class='button_s'"." onclick='location.href=(\"delete.php?id=".$row['id']."\");'".">删除</button><hr>
            ");
        }
    }else{
        echo("没有任何租赁");
    }
    $conn->close();
}elseif(isset($_REQUEST['p']) and !empty($_REQUEST['p'])){
    $p = $_REQUEST['p'];
    include 'sql_config.php';
    $sql = "SELECT * FROM rent WHERE name='$p'";
    $res = $conn->query($sql);
    if($res->num_rows > 0){
        while($row = $res->fetch_assoc()){
            $start_time = new DateTime($row['start_time']);
            $deathline = new DateTime($row['deathline']);
            $timec = date_diff($start_time,$deathline);
            $time = $timec -> format('%a');
            echo("<p>租赁人:".$row['name']."<br />租赁物品:".$row['w']."<br />物品价格:".$row['jiage']."￥/日<br />"."租赁时长:$time 天<br />应付费:".$time*$row['jiage']."￥"."<br />租赁时间:".$row['start_time']."到".$row['deathline']."<br /><button class='button_b'"." onclick='location.href=(\"update.php?id=".$row['id']."\");'".">编辑</button><button class='button_s'"." onclick='location.href=(\"delete.php?id=".$row['id']."\");'".">删除</button><hr>
            ");
        }
    }else{
        echo("没有任何租赁");
    }
    $conn->close();
}
?>